package com.xmtlxx.oss.service.impl;

import com.xmtlxx.common.utils.image.ImageUtils;
import com.xmtlxx.oss.entity.UploadResult;
import com.xmtlxx.oss.enumd.OssEnumd;
import com.xmtlxx.oss.exception.OssException;
import com.xmtlxx.oss.properties.OssProperties;
import com.xmtlxx.oss.service.abstractd.AbstractOssStrategy;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;

/**
 * 本地存储策略
 *
 * @author xmtlxx
 */
@Component
public class LocalUploadStrategy extends AbstractOssStrategy {

    @Override
    public void init(OssProperties ossProperties) {
        super.init(ossProperties);
        try {
            createBucket();
        } catch (Exception e) {
            throw new OssException("本地存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
        }
        isInit = true;
    }

    @Override
    public void createBucket() {
        try {
            String bucketName = properties.getBucketName();
            if (!new File(bucketName).exists()) {
                new File(bucketName).mkdirs();
            }
        } catch (Exception e) {
            throw new OssException("创建本地Bucket失败, 请核对本地配置信息:[" + e.getMessage() + "]");
        }
    }

    @Override
    public OssEnumd getServiceType() {
        return OssEnumd.LOCAL;
    }

    @Override
    public UploadResult upload(byte[] data, String path, String fileName, Map<String,Object> params) {
        return upload(new ByteArrayInputStream(data), path, fileName,params);
    }

    @Override
    public UploadResult upload(InputStream inputStream, String path, String fileName, Map<String,Object> params) {
        try {
            String bucketName = properties.getBucketName();
            bucketName = bucketName +"/"+path;
            String filePath = bucketName.substring(0,bucketName.lastIndexOf("/"));
            if (!new File(filePath).exists()) {
                new File(filePath).mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(bucketName);
            byte[] b = new byte[1024];
            while ((inputStream.read(b)) != -1) {
                fos.write(b);// 写入数据
            }
            inputStream.close();
            fos.close();// 保存数据
            if(params != null && params.get("mark") != null && params.get("mark").equals(1) && params.get("text") != null){
                ImageUtils.pressText(bucketName,params.get("text").toString(),"微软雅黑", Font.BOLD,35,Color.WHITE,20,1f,38);
            }
        } catch (Exception e) {
            throw new OssException("上传文件失败，请检查本地配置信息:[" + e.getMessage() + "]");
        }
        return UploadResult.builder().url(getEndpointLink() + "/" + path).filename(path).bucket(properties.getBucketName()).build();
    }

    @Override
    public void delete(String path) {
        path = path.replace(getEndpointLink() + "/", "");
        try {
            File f = new File(properties.getBucketName()+"/"+path);
            if (f.exists()) {
                f.delete();
            }
        } catch (Exception e) {
            throw new OssException("删除文件失败，请检查本地配置信息:[" + e.getMessage() + "]");
        }
    }

    @Override
    public UploadResult uploadSuffix(byte[] data, String suffix, String contentType, Map<String,Object> params) {
        return upload(data, getPath(properties.getPrefix(), suffix), contentType,params);
    }

    @Override
    public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType, Map<String,Object> params) {
        return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType,params);
    }

    @Override
    public String getEndpointLink() {
        String endpoint = properties.getEndpoint();
        StringBuilder sb = new StringBuilder(endpoint);
        return sb.toString();
    }

    @Override
    public UploadResult uploadThumbnail(byte[] data, String path, String contentType, Map<String, Object> params) {
        return upload(new ByteArrayInputStream(data), path, contentType,params);
    }
}
